package com.asset.dispoal.business.appoint.service.command;

import com.asset.dispoal.business.appoint.domain.db.entity.CaseAppoint;
import com.asset.dispoal.business.appoint.domain.db.mapper.AppointMapper;
import com.asset.dispoal.business.appoint.domain.dto.IdCaseState;
import com.asset.dispoal.business.appoint.dto.AppointDownloadOrderRequest;
import com.asset.dispoal.business.bsCase.domain.db.entity.CaseStateRecord;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;

@Component
@Slf4j
public class AppointDownloadOrderCommand implements BaseCommand<AppointDownloadOrderRequest, Result> {

    @Autowired
    AppointMapper appointMapper;

    @Autowired
    BsCaseMapper caseMapper;

    @Autowired
    BaseIdeable<String> snowFlake;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result execute(AppointDownloadOrderRequest request) {
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        List<IdCaseState> idCaseStateList = appointMapper.queryCaseIdListByAppointId(request.getId());
        AgreementCaseSateMenu waitRecvOrder = AgreementCaseSateMenu.WAIT_RECV_ORDER;
        for (IdCaseState idCaseState : idCaseStateList) {
            if (waitRecvOrder.getLabel().equals(idCaseState.getState())) {
                break;
            }
            caseMapper.updateFormulaId(idCaseState.getId(), request.getFormulaId());
            caseMapper.updateCaseState(idCaseState.getId(), waitRecvOrder.getLabel(), waitRecvOrder.getState(), userId);
            CaseStateRecord caseStateRecord = new CaseStateRecord();
            caseStateRecord.setId(snowFlake.generateId());
            caseStateRecord.setOldState(idCaseState.getState());
            caseStateRecord.setNewState(waitRecvOrder.getLabel());
            caseStateRecord.setCaseId(idCaseState.getId());
            caseStateRecord.setCreateUser(userId);
            caseMapper.insertStateRecord(caseStateRecord);
        }
        CaseAppoint caseAppoint = new CaseAppoint();
        caseAppoint.setId(request.getId());
        caseAppoint.setLitigationTemplateId(request.getLitigationTemplateId());
        caseAppoint.setDownloadMode(request.getDownloadMode());
        caseAppoint.setUseReply(request.getUseReply());
        caseAppoint.setTransferTemplateId(request.getTransferTemplateId());
        caseAppoint.setUseUploadVerify(request.getUseUploadVerify());
        caseAppoint.setFormulaId(request.getFormulaId());
        caseAppoint.setDownloadOrderTime(DateTimeUtil.currentDate());
        caseAppoint.setRequire2(request.getRequire2());
        appointMapper.downloadOrder(caseAppoint);
        return Result.ok();
    }
}
